package POI_Excel;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;


public class PoiCreateExcel {
private static List<Students> getStudent() throws Exception{   
		
	List<Students> list = new ArrayList<Students>();  
		SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");                  
		 Students user1 = new Students(1,"张三",16,df.parse("1997-03-12"));       
		 Students user2 = new Students(2,"李四",17,df.parse("1996-08-12"));       
		 Students user3 = new Students(3,"王武",26,df.parse("1985-11-12"));       
		 list.add(user1);       
		 list.add(user2);       
		 list.add(user3);             
		 return list;    
		 } 
         
public static void main(String[] args) throws Exception {        
         HSSFWorkbook wb = new HSSFWorkbook();  
        
		 HSSFSheet sheet = wb.createSheet("学生表2");     
		 //设置每个sheet每一行的宽度,自动
		 sheet.autoSizeColumn(40, true);	 
		 HSSFCellStyle cellstyle = wb.createCellStyle(); 
		 //设置对齐方式上下ALGIN对齐和左右VERTICAL对齐
		 cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		 cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		 //设置单元格边框的粗细
		 cellstyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
		 cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		 cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		 cellstyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
		 //设置单元格边框的颜色
		 cellstyle.setBottomBorderColor(IndexedColors.RED.getIndex());
		 cellstyle.setTopBorderColor(IndexedColors.RED.getIndex());
		 cellstyle.setLeftBorderColor(IndexedColors.RED.getIndex());
		 cellstyle.setRightBorderColor(IndexedColors.RED.getIndex());
		 //设置单元格的背景颜色
		 cellstyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
		 cellstyle.setFillForegroundColor(IndexedColors.BLUE.getIndex()); 
		 cellstyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		 //设置字体风格
		 Font font=wb.createFont();
		 font.setColor(HSSFColor.BLACK.index);
		 font.setFontName("隶书");
		 font.setItalic(false);  
		 cellstyle.setFont(font);
		 HSSFRow row = sheet.createRow(0); 
		 //设置行长度自动             
         //row.setHeight((short)500);  
         row.setHeightInPoints(20);  
         //row.setZeroHeight(true); 
		 HSSFCell cell=row.createCell(0);
		 cell.setCellValue("ID"); 
		 cell.setCellStyle(cellstyle);   
         cell=row.createCell(1);
		 cell.setCellValue("姓名	"); 
		 cell.setCellStyle(cellstyle);     
         cell=row.createCell(2);
         cell.setCellValue("年龄"); 
         cell.setCellStyle(cellstyle);         
         cell=row.createCell(3);
         cell.setCellValue("出生日期");
         cell.setCellStyle(cellstyle);              
          List<Students> list = getStudent();          
          for(int i=0;i<list.size();i++){  
         
		      	
	     row = sheet.createRow(i+1);  
	     
	    Students stu = (Students) list.get(i);  
	    HSSFCell cell0=row.createCell(0);
            cell0.setCellStyle(cellstyle);  
            cell0.setCellValue(stu.getId());
            cell0=row.createCell(1);
            cell0.setCellStyle(cellstyle);
   	        cell0.setCellValue(stu.getName());  
	        cell0= row.createCell(2);
	        cell0.setCellStyle(cellstyle);
	        cell0.setCellValue(stu.getAge());  
	       cell0=  row.createCell(3);
	       cell0.setCellStyle(cellstyle);
	       cell0.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBrithday()));           
          }    
        try {          
                   FileOutputStream fos = new FileOutputStream("E://学生表3.xls");         
                             wb.write(fos);           
                               fos.close();        
                     } catch (Exception e) {         
	                      e.printStackTrace();        
	                      }    
} 
		 }

